package com.yingxin.yx.framework.web.converter;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.core.convert.converter.Converter;

import java.util.Date;

/**
 * @author fanchao
 */
public class GlobalDateConverters implements Converter<String, Date> {

    private static String[] parsePatterns = {"MM/dd/yyyy", "MM/dd/yyyy HH:mm:ss",
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm"};

    @Override
    public Date convert(String source) {
        try {
            Date target = DateUtils.parseDate(source, parsePatterns);
            return target;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
